<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 200px;"
        :placeholder="$t('请输入用户名称')" />
      <el-button v-permission="['GET /admin/p/feedback_feedback_index']" class="filter-item" type="info" icon="el-icon-search"
        @click="handleFilter">{{ $t('Search') }}</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" :element-loading-text="$t('Searching')" border fit
      highlight-current-row>
      <el-table-column type="index" :index="getIndex" width="50"></el-table-column>


      <el-table-column align="center" :label="$t('用户账号')" prop="username" />
      <el-table-column align="center" :label="$t('昵称')" prop="nickname" />

      <el-table-column align="center" :label="$t('手机号')" prop="mobile" />

      <el-table-column align="center" :label="$t('邮箱')" prop="email" />

      <el-table-column align="center" :label="$t('反馈时间')" prop="addTime" />

      <el-table-column align="center" :label="$t('反馈内容')" prop="content" />

      <el-table-column align="center" :label="$t('回复内容')" prop="reply" />

      <el-table-column align="center" :label="$t('Status')" prop="status">
        <template slot-scope="scope">
          <el-badge is-dot v-if="scope.row.newFeedback">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'error'">{{ statusMap[scope.row.status] }}</el-tag>
          </el-badge>
          <el-tag v-else :type="scope.row.status == 1 ? 'success' : 'error'">{{ statusMap[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column  align="center" :label="$t('Operate')" width="200" class-name="small-padding fixed-width">
        <template slot="header" slot-scope="{row}">
          {{$t('Operate')}}
          <i class="el-icon-setting"style="color: #d35e32"></i>
        </template>
        <template slot-scope="scope">
          <el-button v-permission="['GET /admin/p/feedback_feedback_reply']" icon="el-icon-thumb" type="primary" plain1
            @click="handleReply(scope.row)" :disabled="scope.row.status != 0">{{ $t('去处理') }}</el-button>
          <el-button v-permission="['GET /admin/p/feedback_feedback_ignore']" icon="el-icon-close-notification" type="info" plain1 size="mini"
            @click="handleIgnore(scope.row)" :disabled="scope.row.status != 0">{{ $t('忽略') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <el-tooltip placement="top" :content="$t('Back_to_top')">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

    <!-- 回复反馈对话框 -->
    <el-dialog :visible.sync="feedbackDialogVisible" :title="$t('Undo')">
      <el-form ref="feedbackForm" :model="feedbackForm" status-icon label-position="left" label-width="100px"
        style="width: 400px; margin-left:50px;">
        <el-form-item :label="$t('内容')" prop="reply">
          <el-input v-model="feedbackForm.reply" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain1 round icon="el-icon-error" @click="cancelFeedback">{{ $t('Cancel') }}</el-button>
        <el-button plain1 round icon="el-icon-success" type="primary" @click="doReply">{{ $t('Confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ElSelectDropdown from 'element-ui/packages/select/src/select-dropdown'
import { listFeedback, replyFeedback, ignoreFeedback } from '@/api/feedback'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: "feedbackList",
  components: { ElSelectDropdown, BackToTop, Pagination },
  data() {
    const statusMap = [this.$t('未处理'), this.$t('Processed'), this.$t('已忽略')]
    return {
      statusMap,
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      feedbackDialogVisible: false,
      feedbackForm: {
        id: undefined,
        userId: undefined,
        reply: ""
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listFeedback(this.listQuery).then(response => {
        const records = response.data.data;
        this.list = records.list;
        this.total = records.total;

        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.getList()
    },
    handleReply(row) {
      this.feedbackForm.id = row.id
      this.feedbackForm.userId = row.userId
      this.feedbackDialogVisible = true
      // this.$confirm(this.$t('是否') + this.$t('Processed') + "?", this.$t('Hint'), {
      //   confirmButtonText: this.$t('Confirm'),
      //   cancelButtonText: this.$t('Cancel'),
      //   type: 'warning'
      // }).then(() => {
      //   this.doReply(row)
      // });
    },
    cancelFeedback(){
        this.feedbackForm.reply = '';
        this.feedbackDialogVisible = false
    },
    doReply() {
      replyFeedback(this.feedbackForm).then(response => {
        this.$notify.success({
          title: this.$t('Success!'),
          message: this.$t('Success!')
        })
        this.feedbackDialogVisible = false
        this.feedbackForm.reply = '';
        this.getList()

      }).catch(response => {
        this.$notify.error({
          title: this.$t('Failed'),
          message: this.$t(response.data.errmsg)
        })
      })
    },
    handleIgnore(row) {
      this.$confirm(this.$t('是否') + this.$t('忽略') + "?", this.$t('Hint'), {
        confirmButtonText: this.$t('Confirm'),
        cancelButtonText: this.$t('Cancel'),
        type: 'warning'
      }).then(() => {
        this.doIgnore(row)
      });
    },
    doIgnore(row) {
      let data = {
        id: row.id
      }
      ignoreFeedback(data).then(response => {
        this.$notify.success({
          title: this.$t('Success!'),
          message: this.$t('Success!')
        })
        this.getList()
      }).catch(response => {
        this.$notify.error({
          title: this.$t('Failed'),
          message: this.$t(response.data.errmsg)
        })
      })
    },
    getIndex(index) {
      return (index + 1) + (this.listQuery.page - 1) * this.listQuery.limit;
    }
  }
}
</script>

<style scoped>
/deep/ .is-dot {
  right: 0 !important;
  top: 0 !important;
  transform: translate(16%, 1%) !important;
}

</style>
